package com.hc.concurrent_likes.controller;

import com.hc.concurrent_likes.entity.MoodDTO;
import com.hc.concurrent_likes.service.MoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/mood")
public class MoodController {

    @Autowired
    @Qualifier("moodService")
    private MoodService moodService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<MoodDTO> moodDTOS = moodService.findAll();
        model.addAttribute("moods", moodDTOS);
        return "mood";
    }

    @RequestMapping("/{moodId}/praise")
    public String praise(Model model, @PathVariable("moodId") String moodId,
                         @RequestParam("userId") String userId){

        //点赞
        boolean isPraise = moodService.praiseMood(userId, moodId);

        //查询所有用户
        List<MoodDTO> moodDTOList = moodService.findAll();

        model.addAttribute("moods", moodDTOList);
        model.addAttribute("isPraise", isPraise);
        return "mood";
    }

    @RequestMapping("/{moodId}/praiseForRedis")
    public String praiseForRedis(Model model, @PathVariable("moodId")String moodId
            , @RequestParam("userId") String userId){
        //点赞
        boolean isPraise = moodService.praiseMoodForRedis(moodId, userId);
        //查询所有用户
        List<MoodDTO> moodDTOList = moodService.findAllForRedis();
//        for (MoodDTO moodDTO : moodDTOList) {
//            System.out.println(moodDTO.getPraiseNum());
//        }
        model.addAttribute("moods", moodDTOList);
        model.addAttribute("isPraise", isPraise);
        return "mood";
    }
}
